<?php
/**
 * 缩略图
 * User: dxk
 * Date: 2020/07/17
 */
namespace App\Http\Controllers\Service;

use App\Http\Controllers\Controller;
use App\Library\Service\AliCloud\OSS;
use App\Library\Service\ThumbPic;
use Illuminate\Http\Request;

class ThumbPicController extends Controller
{
    public function index(Request $request)
    {
        $path = trim($request->input('path'));
        if (str_starts_with($path, 'devfile')) {
            $path = str_replace('devfile/prod', 'dev', $path);
            $url = 'https://gemside.oss-cn-beijing.aliyuncs.com/' . $path;
            return file_get_contents($url);
        }
        $dst_file = public_path('attached' . DIRECTORY_SEPARATOR . $path);
        $thumpPic = new ThumbPic();
        $pic_path = $thumpPic->makeAutoThump($dst_file);
        if (!$pic_path) {
            $this->echoDefaultPic();
        }
        $size_info = getimagesize($pic_path);
        if (!$size_info) {
            $this->echoDefaultPic();
        }
        header('Content-type: ' . $size_info["mime"]);
        echo file_get_contents($pic_path);
        die();
    }

    private function echoDefaultPic()
    {
        http_response_code(404);
        die;
        $default_pic = public_path('resource/img/common/default_s.gif');
        header('Content-type: image/gif');
        echo file_get_contents($default_pic);
        die;
    }
}
